//
//  THomeHeadCollectionView.m
//  LoveShare
//
//  Created by ways on 2017/4/20.
//  Copyright © 2017年 tikeyc. All rights reserved.
//

#import "THomeHeadCollectionView.h"

#import "JYJEssenceViewController.h"

#import "TFindDriverListViewController.h"
#import "TReleaseFindDriverViewController.h"

#import "TFindPassengerListViewController.h"
#import "TReleaseFindPassengerViewController.h"

#import "THomeHelperListViewController.h"
#import "TReleaseHomeHelperViewController.h"

#import "TShoppingListViewController.h"
#import "TReleaseShoppingViewController.h"

#import "TSellThingsListViewController.h"
#import "TReleaseSellThingsViewController.h"

#import "THealthyLifeListViewController.h"
#import "TReleaseHealthyLifeViewController.h"

#import "THomeHeadCollectionViewCell.h"

static NSString *homeItemIndentifier = @"homeItem";

@interface THomeHeadCollectionView ()<UICollectionViewDataSource,UICollectionViewDelegate>{
    UIView *_bottomLineView;
}


@property (nonatomic,copy) NSArray *itemsTitles;
@property (nonatomic,copy) NSArray *itemsIconImageNames;

@end

@implementation THomeHeadCollectionView

+ (instancetype)initTHomeHeadCollectionView {
    UICollectionViewFlowLayout *layout = [THomeHeadCollectionView getFlowLayout];
    THomeHeadCollectionView *homeHeadCollectionView = [[THomeHeadCollectionView alloc] initWithFrame:CGRectMake(0, 0, TScreenWidth, 220) collectionViewLayout:layout];
    return homeHeadCollectionView;
}

- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout {
    self = [super initWithFrame:frame collectionViewLayout:layout];
    if (self) {
        
        [self initProperty];
        
    }
    return self;
}


- (void)awakeFromNib {
    [super awakeFromNib];
    
    [self initProperty];
}

#pragma mark - init

- (void)initProperty {
    self.dataSource = self;
    self.delegate = self;
    
    self.contentInset = UIEdgeInsetsMake(0, 10, 0, 10);
    self.scrollEnabled = NO;
    //
    self.backgroundColor = [UIColor clearColor];
    //
    [self registerNib:[UINib nibWithNibName:@"THomeHeadCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:homeItemIndentifier];
    
    _bottomLineView = [[UIView alloc] initWithFrame:CGRectMake(-10, self.height - 1, TScreenWidth, 1)];
    _bottomLineView.backgroundColor = TColor_RGB(211, 211, 211);
    [self addSubview:_bottomLineView];
    
    _itemsTitles = @[@"找司机",@"找乘客",@"令居帮手",
                     @"买东西",@"卖东西",@"健康生活",];
    _itemsIconImageNames = @[@"nav_ico_car_29x29_",@"home_ico_xiche_29x29_",@"nav_ico_weixiu_29x29_",
                             @"home_ico_wuliu_29x29_",@"nav_ico_banyun_29x29_",@"nav_ico_baomu_29x29_"];
}


+ (UICollectionViewFlowLayout *)getFlowLayout {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(TScreenWidth/4, 100);
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10;
    
    return layout;
}

- (void)setIsTabReleaseSelectedComing:(BOOL)isTabReleaseSelectedComing {
    _isTabReleaseSelectedComing = isTabReleaseSelectedComing;
    
    _bottomLineView.hidden = _isTabReleaseSelectedComing;
}

#pragma mark - UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _itemsTitles.count;
}


- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
    THomeHeadCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:homeItemIndentifier forIndexPath:indexPath];
    
//    if (indexPath.row % 2) {
//        cell.backgroundColor = [UIColor redColor];
//    }else cell.backgroundColor = [UIColor orangeColor];
    
    cell.iconTitleLabel.text = _itemsTitles[indexPath.row];
    cell.iconImageView.image = [UIImage imageNamed:_itemsIconImageNames[indexPath.row]];
    return cell;
}

#pragma mark - UICollectionViewDelegate

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    cell.transform = CGAffineTransformMakeScale(0.0, 0.0);
    NSTimeInterval duration = 0.5;
    [UIView animateWithDuration:duration
                          delay:(indexPath.row * duration) * 0.1
         usingSpringWithDamping:0.5
          initialSpringVelocity:1.0
                        options:UIViewAnimationOptionCurveLinear
                     animations:^{
                         cell.transform = CGAffineTransformIdentity;
                     }
                     completion:^(BOOL finished) {
                         
                     }];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
//    JYJEssenceViewController *carVC = [[JYJEssenceViewController alloc] init];
//    [self.homeVC.navigationController pushViewController:carVC animated:YES];
    
    NSString *title = _itemsTitles[indexPath.row];
    switch (indexPath.row) {
        case THomeHeadItemTypeFindDriver:
        {
            if (_isTabReleaseSelectedComing) {
                TReleaseFindDriverViewController *driverVC = [[TReleaseFindDriverViewController alloc] init];
                driverVC.title = title;
                [self.homeVC.navigationController pushViewController:driverVC animated:YES];
            } else {
                TFindDriverListViewController *driverListVC = [[TFindDriverListViewController alloc] init];
                driverListVC.title = title;
                [self.homeVC.navigationController pushViewController:driverListVC animated:YES];
            }
            
        }
            break;
        case THomeHeadItemTypeFindPassenger:
        {
            if (_isTabReleaseSelectedComing) {
                TReleaseFindPassengerViewController *passengerVC = [[TReleaseFindPassengerViewController alloc] init];
                passengerVC.title = title;
                [self.homeVC.navigationController pushViewController:passengerVC animated:YES];
            } else {
                TFindPassengerListViewController *passengerVC = [[TFindPassengerListViewController alloc] init];
                passengerVC.title = title;
                [self.homeVC.navigationController pushViewController:passengerVC animated:YES];
            }
            
        }
            break;
        case THomeHeadItemTypeHomeHelper:
        {
            [TAlertView showWithTitle:nil message:@"尽情期待" cancelButtonTitle:@"确定" otherButtonTitles:nil type:UIAlertControllerStyleAlert andParentView:nil andAction:^(NSInteger buttonIndex) {
                
            }];
//            if (_isTabReleaseSelectedComing) {
//                TReleaseHomeHelperViewController *homeHelperVC = [[TReleaseHomeHelperViewController alloc] init];
//                homeHelperVC.title = title;
//                [self.homeVC.navigationController pushViewController:homeHelperVC animated:YES];
//            } else {
//                THomeHelperListViewController *homeHelperVC = [[THomeHelperListViewController alloc] init];
//                homeHelperVC.title = title;
//                [self.homeVC.navigationController pushViewController:homeHelperVC animated:YES];
//            }
            
        }
            break;
        case THomeHeadItemTypeShopping:
        {
            if (_isTabReleaseSelectedComing) {
                TReleaseShoppingViewController *shoppingVC = [[TReleaseShoppingViewController alloc] init];
                shoppingVC.title = title;
                [self.homeVC.navigationController pushViewController:shoppingVC animated:YES];
            } else {
                
                TShoppingListViewController *shoppingVC = [[TShoppingListViewController alloc] init];
                shoppingVC.title = title;
                [self.homeVC.navigationController pushViewController:shoppingVC animated:YES];
            }
            
        }
            break;
        case THomeHeadItemTypeSellThings:
        {
            if (_isTabReleaseSelectedComing) {
                TReleaseSellThingsViewController *sellVC = [[TReleaseSellThingsViewController alloc] init];
                sellVC.title = title;
                [self.homeVC.navigationController pushViewController:sellVC animated:YES];
            } else {
                TSellThingsListViewController *sellVC = [[TSellThingsListViewController alloc] init];
                sellVC.title = title;
                [self.homeVC.navigationController pushViewController:sellVC animated:YES];
            }
            
        }
            break;
        case THomeHeadItemTypeHealthyLifeStyle:
        {
            [TAlertView showWithTitle:nil message:@"尽情期待" cancelButtonTitle:@"确定" otherButtonTitles:nil type:UIAlertControllerStyleAlert andParentView:nil andAction:^(NSInteger buttonIndex) {
                
            }];
//            if (_isTabReleaseSelectedComing) {
//                TReleaseHealthyLifeViewController *healthyVC = [[TReleaseHealthyLifeViewController alloc] init];
//                healthyVC.title = title;
//                [self.homeVC.navigationController pushViewController:healthyVC animated:YES];
//            } else {
//                THealthyLifeListViewController *healthyVC = [[THealthyLifeListViewController alloc] init];
//                healthyVC.title = title;
//                [self.homeVC.navigationController pushViewController:healthyVC animated:YES];
//            }
            
        }
            break;
            
        default:
            break;
    }

    
}

@end







